package stack_queue.stack;

import java.util.NoSuchElementException;


public class Stack <E>{
    private E[] elemenDate;
    private int size;
    public Stack(){
        elemenDate = (E[])new Object[10];
    }
    public int getSize(){
        return size;
    }
    //入栈操作
    public void push(E value){
        elemenDate[size++] = value;
    }
    //出栈操作,返回之前栈顶元素
    public E pop(){
        if(getSize() == 0){
            throw new NoSuchElementException("栈为空！");
        }
        E oldValue = elemenDate[size-1];
        size--;
        elemenDate[size] = null;
        return oldValue;
    }
    //查看栈顶元素
    public E peek(){
        if (getSize() == 0){
            throw new NoSuchElementException("栈为空！");
        }
        return elemenDate[size-1];
    }
    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        for(int i = size-1;i >= 0; --i){
            System.out.println("|"+elemenDate[i]+"|");
        }
        return sb.toString();
    }



}
